package com.example.model23.test4;

import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

public class WeightCalculatorTest {

    public static void main (String[] args) {
        // 设置权重
        Map<String, Integer> weights = new HashMap<>();
        weights.put("a", 10);
        weights.put("b", 20);
        weights.put("c", 30);

        WeightCalculator calculator = new WeightCalculator(weights);

        // 运行多次测试，统计结果
        int iterations = 10000;
        Map<String, Integer> resultCounts = new HashMap<>();
        for (String key : weights.keySet()) {
            resultCounts.put(key, 0);
        }

        for (int i = 0; i < iterations; i++) {
            String result = calculator.fetchCalcResult();
            resultCounts.put(result, resultCounts.get(result) + 1);
        }

        // 检查结果是否符合权重比例（允许一定的误差）
        double aPercentage = (double) resultCounts.get("a") / iterations;
        double bPercentage = (double) resultCounts.get("b") / iterations;
        double cPercentage = (double) resultCounts.get("c") / iterations;

        // 预期比例（10:20:30）转换为百分比
        double expectedAPercentage = 10.0 / 60.0;
        double expectedBPercentage = 20.0 / 60.0;
        double expectedCPercentage = 30.0 / 60.0;

        // 允许5%的误差
        double errorMargin = 0.05;

        assertTrue(Math.abs(aPercentage - expectedAPercentage) <= errorMargin);
        assertTrue(Math.abs(bPercentage - expectedBPercentage) <= errorMargin);
        assertTrue(Math.abs(cPercentage - expectedCPercentage) <= errorMargin);
    }
}